﻿using Simple.Common.Core.Hosting;
using Simple.Common.EntityFrameworkCore.Repository;
using Simple.Common.Uow.Uow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Common.EntityFrameworkCore.DbProviders
{
    public class DbProvider : IDbProvider
    {
        public async virtual  ValueTask DisposeAsync()
        {
            await DisposeAsyncCore();
            GC.SuppressFinalize(this);
        }
        protected virtual async ValueTask DisposeAsyncCore()
        {
            await Task.CompletedTask;
        }
        public IUnitOfWork GetUnitOfWork(params IBaseRepository[] repositories)
        {
            //获取当前工作单元，存在新增db，不存在初始化巩工作单元
            var uow= ServiceProviderHelper.GetService<IUnitOfWork>();
            uow.AppendDbContext(repositories.Select(x => x.DBContext).ToList());
            return uow;
        }
    }
}
